﻿using Microsoft.AspNetCore.Mvc;
using SmartSiteManagementSystem.IServices;
using SmartSiteManagementSystem.Models.Equipment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using System.Collections;
using Newtonsoft.Json;
using SmartSiteManagementSystem.Models.DTO;

namespace SmartSiteManagementSystem.AdminWeb.Controllers
{
    public class EquipmentController : AdminBaseControllers
    {
        //注入后台服务
        private readonly IAdminService _adminService;
        private readonly IEquipmentService _equipmentService;
        private readonly ILogger<EquipmentController> _logger;
        public EquipmentController(IAdminService adminService,
            IEquipmentService equipmentService,
            ILogger<EquipmentController> logger ) : base(adminService)
        {
            _adminService = adminService;
            _equipmentService = equipmentService;
            _logger = logger;
        }

        public IActionResult TowerCraneIndex()
        {
            return View();
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        public JsonResult GetTowerCraneList(string searchKey, int page = 0, int rows = 50)
        {
            if (page < 1)
            {
                page = 1;
            }
            page = (page - 1) * rows;

            Hashtable param = new Hashtable();
            param.Add("is_delete", 0);//正常 0 false
            int count = _equipmentService.GetTowerCraneInfoListCount(param);
            IList<TowerCraneInfo> list = _equipmentService.GetTowerCraneInfoList(param, "", page, rows);
            var data = new
            {
                total = count,
                rows = list
            };
            return Json(data);
        }




        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="towerCraneId"></param>
        public JsonResult ajaxDeleteTowerCrane(int towerCraneId)
        {
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (towerCraneId > 0)
            {
                TowerCraneInfo info = _equipmentService.GetTowerCraneInfoById(towerCraneId);
                if (info != null)
                {
                    info.IsDelete = true;
                    result.IsSuccess = _equipmentService.UpdateTowerCraneInfo(info);
                    if (result.IsSuccess)
                    {
                        result.Msg = "操作成功!";
                    }
                }
            }
            return Json(result);
        }


        /// <summary>
        /// 添加或修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrEditTowerCrane(int id = 0)
        {
            TowerCraneInfo info = new TowerCraneInfo();
            if (id > 0)
            {
                info = _equipmentService.GetTowerCraneInfoById(id);
            }
            ViewBag.towerCraneInfo = info;

            return View();
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="TowerCraneId"></param>
        /// <param name="DemandingPlaceName"></param>
        /// <param name="PositionX"></param>
        /// <param name="PositionY"></param>
        /// <param name="PositionZ"></param>
        /// <param name="Descript"></param>
        public JsonResult ajaxAddOrEditTowerCrane(int TowerCraneId,string TowerCraneName,double OperatingRadius, int Weight,
            double PositionX, double PositionY, double PositionZ,
             string Descript)
        {
            TowerCraneInfo info = null;
            ResultInfo result = new ResultInfo();
            result.IsSuccess = false;
            result.Code = 200;
            result.Msg = "操作失败!";
            if (TowerCraneId > 0)
            {
                info = _equipmentService.GetTowerCraneInfoById(TowerCraneId);
            }
            else
            {
                info = new TowerCraneInfo();
            }
            info.IsDelete = false;
            info.OperatingRadius = OperatingRadius;
            info.Weight = Weight;
            info.PositionX = PositionX;
            info.PositionY = PositionY;
            info.PositionZ = PositionZ;
            info.TowerCraneName = TowerCraneName;

            info.Descript = Descript;
            if (TowerCraneId > 0)
            {
                result.IsSuccess  = _equipmentService.UpdateTowerCraneInfo(info);

            }
            else
            {
                TowerCraneId = _equipmentService.InsertTowerCraneInfo(info);
                result.IsSuccess  = TowerCraneId > 0 ? true : false;

            }
            if (result.IsSuccess)
            {
                result.Msg = "操作成功!";
            }
            return Json(result);
        }

    }
}
